﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BDSim
{
    public class ReceiverDataProvider : BDSimDataProvider
    {
        private CReceiver _Parent;

        public CReceiver Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }

        #region 命令定义
        public override void DefineCommand()
        {
            MenuCommand mcdFreSet = new MenuCommand() { Header = "接收频率设置" };
            mcdFreSet.DefineExcuteCommand(mcdFreSet_Execute, mcdFreSet_CanExecute);
            MenuCommandList.Add(mcdFreSet);

            MenuCommand mcdObsDataReport = new MenuCommand() { Header = "观测数据报告" };
            mcdObsDataReport.DefineExcuteCommand(mcdObsDataReport_Execute, mcdObsDataReport_CanExecute);
            MenuCommandList.Add(mcdObsDataReport);

            MenuCommand mcdDelete = new MenuCommand() { Header = "删除" };
            mcdDelete.DefineExcuteCommand(mcdDelete_Execute, mcdDelete_CanExecute);
            MenuCommandList.Add(mcdDelete);
        }
        #endregion

        #region 命令执行方法
        #region 接收频率设置
        void mcdFreSet_Execute(object param)
        {
            RecFreSetWnd wndFreSet = new RecFreSetWnd();
            wndFreSet.RecType_Fre = Parent.RecType_Fre;
            if (wndFreSet.ShowDialog() == true)
            {
                if (wndFreSet.One2AllFlag == true)
                {
                    foreach (var user in (Parent.Parent.Parent as UserSegment).UserModelList)
                    {
                        foreach (var item in user.RecCollection)
                        {
                            item.RecType_Fre = wndFreSet.RecType_Fre;
                        }
                    }
                }
                else
                {
                    Parent.RecType_Fre = wndFreSet.RecType_Fre;
                }
            }
        }

        bool mcdFreSet_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }
        #endregion 接收频率设置

        #region 观测数据

        void mcdObsDataReport_Execute(object param)
        {
            DataReportView dataPreview = new DataReportView();
            //获取卫星的名称列表
            List<string> remoteNodeNameList = new List<string>();
            foreach (var item in (Parent.Parent.Parent.Parent.SegmentColletion[0] as SpaceSegment).AllSatellite)
            {
                remoteNodeNameList.Add(item.Name);
            }
            double[] Fres = new double[Parent.RecFre.Count()];
            for (int i = 0; i < Fres.Count(); i++)
            {
                double frequency = 0.0;
                switch ((Parent.RecFre[i]))
                {
                    case EFrePoint.BD_B1:
                        frequency = 1561.098;
                        break;
                    case EFrePoint.BD_B2:
                        frequency = 1207.14;
                        break;
                    case EFrePoint.BD_B3:
                        frequency = 1268.52;
                        break;
                    case EFrePoint.GPS_L1:
                        frequency = 1575.42;
                        break;
                    case EFrePoint.GPS_L2:
                        frequency = 1227.60;
                        break;
                    case EFrePoint.GPS_L5:
                        frequency = 1176.45;
                        break;
                    case EFrePoint.GLONASS_L1:
                        frequency = 1602;
                        break;
                    case EFrePoint.GLONASS_L2:
                        frequency = 1246;
                        break;
                    case EFrePoint.GALILEO_E1:
                        frequency = 1575.42;
                        break;
                    case EFrePoint.GALILEO_E5a:
                        frequency = 1176.45;
                        break;
                }
                Fres[i] = frequency;
            }

            List<Dictionary<string, List<ObsReceiverOutput>>> allObsSatsDataDic = new List<Dictionary<string, List<ObsReceiverOutput>>>();
            foreach (var onetime in Parent.ALLObsSatsDataDic_ConType)
            {
                foreach (var con in onetime.Keys)
                {
                    allObsSatsDataDic.Add(onetime[con]);   
                }
            }  
            dataPreview.InitObsData(allObsSatsDataDic, remoteNodeNameList, Parent.Parent.Name + "<" + this.Parent.Name + ">", Fres);
            MainDispatcher.DlgAddLayoutDocument(dataPreview, Parent.Parent.Name + "<" + this.Parent.Name + ">" + "_观测数据");
        }

        bool mcdObsDataReport_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.Run || BDSimObject.GlobalStatus == ESimStatus.End || Scenario._IsTestingMode == false)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region 删除命令执行方法
        /// <summary>
        /// 删除接收机功能命令执行方法
        /// </summary>
        /// <param name="param"></param>
        void mcdDelete_Execute(object param)
        {
            Parent.Parent.RecCollection.Remove(Parent);
        }
        /// <summary>
        /// 删除接收机功能命令可执行控制
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        bool mcdDelete_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        #endregion

        #region 执行方法模板
        /// <summary>
        /// XXX 功能命令执行方法
        /// </summary>
        /// <param name="param"></param>
        void XXX_Execute(object param)
        {

        }
        /// <summary>
        /// XXX功能命令可执行控制
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        bool XXX_CanExecute(object param)
        {
            bool flag = true;

            return flag;
        }

        #endregion

        #endregion
    }
}
